package org.jsola.hr.constant;

import org.jsola.core.validator.IConst;
import org.jsola.json.JsonKit;

import java.util.HashMap;
import java.util.Map;

/**
 * 周期性提醒的单位
 *
 * @author 蔡超
 */
public enum RemindUnitEnum implements IConst {

    /**
     * 天
     */
    DAY("DAY","天"),
    /**
     * 周
     */
    WEEK("WEEK","周"),
    /**
     * 月
     */
    MONTH("MONTH","月"),
    /**
     * 年
     */
    YEAR("YEAR","年");

    private final String value;
    private final String desc;
    /**
     * 枚举值罗列，给swagger接口文档展示用
     */
    public static final String VALUES_STR = "DAY,WEEK,MONTH,YEAR";

     /**
     * 缓存所有value
     */
    private static final Map<String, RemindUnitEnum> LOOKUP = new HashMap<>();

    static {
        for (RemindUnitEnum e : RemindUnitEnum.values()) {
            LOOKUP.put(e.value, e);
        }
    }


    RemindUnitEnum(String value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    /**
     * 根据value查找RemindUnitEnum
     * @param value value
     * @return RemindUnitEnum
     */
    public static RemindUnitEnum find(String value) {
        return LOOKUP.get(value);
    }

    /**
     * 根据desc查找RemindUnitEnum
     * @param desc desc
     * @return RemindUnitEnum
     */
    public static RemindUnitEnum findByDesc(String desc){
        for (RemindUnitEnum e : RemindUnitEnum.values()) {
            if(e.getDesc().equals(desc)){
                return e;
            }
        }
        return null;
    }

    /**
     * 所有枚举转json
     * @return json
     */
    public static String getAllToJson() {
        Map<String,Object> map = new HashMap<>();
        for (RemindUnitEnum e : RemindUnitEnum.values()) {
            map.put("value", e.value);
            map.put("desc", e.desc);
        }
        return JsonKit.toJSONString(map);
    }

    @Override
    public boolean check(Object obj) {
        String value = (String) obj;
        return find(value) != null;
    }

    public String getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
}
